package com.qf.config;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;

/**
 * @author wbzs,
 * @date 2022/10/26,
 * @time 20:09,
 * 自定义过滤器
 */
@Configuration
public class Mygatewayconfig extends AbstractGatewayFilterFactory<Mygatewayconfig.config> {
    @Override
    public GatewayFilter apply(config config) {
        return (exchange,chain)->{
            ServerHttpRequest request = exchange.getRequest();//获取到请求对象
            ServerHttpResponse response = exchange.getResponse();//获取到响应对象
            exchange.mutate().build();//复制一个新的exchange
            System.err.println("我们设置的自定义过滤器执行了"+config.name);
            return chain.filter(exchange);//放行
//            return exchange.getResponse().setComplete();//设置为结束，也就是拦截请求
        };
    }

    //如何自定义过滤器，实际上内部会设置过滤器要执行的代码
public static class config{
        private  String name;

        public config(String name) {
            this.name = name;
        }
    }

//    public static class config{
//
//    }
}
